package com.bangdream.michelia.presenter;

import android.app.Dialog;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.bangdream.michelia.application.AppCurrentUser;
import com.bangdream.michelia.application.AppQuestData;
import com.bangdream.michelia.contract.ExamContract;
import com.bangdream.michelia.entity.BaseEntity;
import com.bangdream.michelia.entity.MyExamBean;
import com.bangdream.michelia.entity.SignUpBean;
import com.bangdream.michelia.entity.exam.AnswerBean;
import com.bangdream.michelia.entity.exam.ExamBean;
import com.bangdream.michelia.entity.exam.ExamQuestBean;
import com.bangdream.michelia.entity.exam.ExamResultDetailsBean;
import com.bangdream.michelia.entity.exam.ExamResultItemBean;
import com.bangdream.michelia.entity.exam.SubjectBean;
import com.bangdream.michelia.entity.exam.SubjectInfoBean;
import com.bangdream.michelia.model.IExamModelImpl;
import com.bangdream.michelia.presenter.base.BasePresenter;
import com.bangdream.michelia.tool.rejava.BaseObserverEntityNormalHttp;
import com.bangdream.michelia.tool.rejava.BaseObserverStringNormalHttp;
import com.bangdream.michelia.tool.retrofithttp.TokenManagerDef;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @date on 2018/8/19 15:00
 * @email 909732184@qq.com
 */
public class ExamPresenter<V> extends BasePresenter<V> {
    private ExamContract.IExamModel model;

    public ExamPresenter() {
        this.model = new IExamModelImpl();
    }

    //获取考试列表
    public void getExamList(final int pageNo, int pageSize, String type, Map paramsMap, Dialog dialog) {
        final ExamContract.IExamView iExamView = getMyView();
        if (iExamView == null)
            return;
        if (paramsMap == null) {
            paramsMap = new HashMap();
        }
        setPd(dialog);
        paramsMap.put("pageNo", pageNo);
        paramsMap.put("pageSize", pageSize);

        if (type.equals("1")) {//在线考试
            paramsMap.put("type", "1");
            paramsMap.put("isFree", "");
            paramsMap.put("isPurchase", "0");
        } else {
            paramsMap.put("itemType", "01");
        }
        Map finalParamsMap = paramsMap;

        TokenManagerDef.work(() ->
                model.getExamList(finalParamsMap,type)).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<List<ExamBean>>(this, pd) {
                    @Override
                    public void onHandleSuccess(List<ExamBean> beans) {
                        iExamView.setExamList(true, pageNo, beans, "请求成功");

                    }

                    @Override
                    public void onHandleError(int code, String message) {
                        super.onHandleError(code, message);
                        iExamView.setExamList(false, pageNo, null, "请求成功");
                    }
                });
    }

    //获取问卷列表
    public void getExamQuList(String type, Dialog dialog) {
        final ExamContract.IExamView iExamView = getMyView();
        if (iExamView == null)
            return;
        setPd(dialog);
        TokenManagerDef.work(() -> model.getExamQuList(type)).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<List<ExamBean>>(this, pd) {
                    @Override
                    public void onHandleSuccess(List<ExamBean> beans) {
                        iExamView.setExamQuList(true, beans, "请求成功");
                    }

                    @Override
                    public void onHandleError(int code, String message) {
                        super.onHandleError(code, message);
                        iExamView.setExamQuList(false, null, "请求失败");
                    }
                });
    }

    //获取问卷题目列表
    public void getExamQuestList(String examId, Dialog dialog) {
        final ExamContract.IExamView iExamView = getMyView();
        if (iExamView == null)
            return;
        setPd(dialog);
        TokenManagerDef.work(() -> model.getExamQusetList(examId)).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<List<ExamQuestBean>>(this, pd) {
                    @Override
                    public void onHandleSuccess(List<ExamQuestBean> beans) {
//                        Log.i("-----", "" + beans.toString());
                        iExamView.setExamQuestList(true, beans, "请求成功");
                    }

                    @Override
                    public void onHandleError(int code, String message) {
                        super.onHandleError(code, message);
                        iExamView.setExamQuestList(false, null, "请求失败");
                    }
                });
    }

    //获取题目列表
    public void getSubjectList(String examId, Dialog dialog) {
        final ExamContract.IExamView iExamView = getMyView();
        if (iExamView == null)
            return;
        setPd(dialog);
        TokenManagerDef.work(() -> model.getSubjectList(examId)).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<List<SubjectBean>>(this, pd) {
                    @Override
                    public void onHandleSuccess(List<SubjectBean> beans) {
                        iExamView.setSubjectList(true, beans, "请求成功");
                    }

                    @Override
                    public void onHandleError(int code, String message) {
                        super.onHandleError(code, message);
                        iExamView.setSubjectList(false, null, "请求成功");
                    }
                });
    }


    /**
     * 获取单个试题信息
     *
     * @param infoId
     * @param getAnswer 1：获取 0：不获取  是否获取答案
     */
    public void getSubjectDetails(String infoId, String getAnswer, Dialog dialog) {
        final ExamContract.IExamView iExamView = getMyView();
        if (iExamView == null)
            return;
        setPd(dialog);
        Map paramsMap = new HashMap();
        paramsMap.put("getAnswer", getAnswer);
        paramsMap.put("infoId", infoId);
        TokenManagerDef.work(() -> model.getSubjectDetails(paramsMap)).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<SubjectInfoBean>(this, pd) {
                    @Override
                    public void onHandleSuccess(SubjectInfoBean beans) {
                        iExamView.setSubjectDetails(true, beans, "请求成功");
                    }

                    @Override
                    public void onHandleError(int code, String message) {
                        super.onHandleError(code, message);
                        iExamView.setSubjectDetails(false, null, "请求成功");
                    }
                });
    }


    /**
     * 初始化考试id
     *
     * @param paperId 试卷ID
     * @param dialog
     */
    public void initExamId(String paperId, Dialog dialog) {
        final ExamContract.IExamView iExamView = getMyView();
        if (iExamView == null)
            return;
        setPd(dialog);
        Map paramsMap = new HashMap();
        paramsMap.put("paperId", paperId);
        paramsMap.put("userId", AppCurrentUser.getInstance().getUserInfo().getId());
        TokenManagerDef.work(() -> model.initExamId(paramsMap)).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<String>(this, pd) {
                    @Override
                    public void onHandleSuccess(String examId) {
                        iExamView.initExamId(true, examId, "请求成功");
                    }

                    @Override
                    public void onHandleError(int code, String message) {
                        super.onHandleError(code, message);
                        iExamView.initExamId(false, null, message);
                    }
                });
    }

    /**
     * 提交答案
     *
     * @param bean
     * @param isFinish 是否交卷
     * @param dialog
     */
    public void actionSubmitExam(AnswerBean bean, final boolean isFinish, Dialog dialog) {
        final ExamContract.IExamView iExamView = getMyView();
        if (iExamView == null)
            return;
        setPd(dialog);
        Map paramsMap = new HashMap();
//        TokenManagerDef.work(() ->
                model.actionSubmitExam(bean).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new BaseObserverStringNormalHttp(this, pd) {
                    @Override
                    public void onHandleSuccess(String stResult) {
                        BaseEntity entity = JSON.parseObject(stResult, BaseEntity.class);
                        if (entity != null) {
                            if (entity.isOk()) {
                                iExamView.actionSubmitExam(true, isFinish, "提交成功");
                            } else {
                                iExamView.actionSubmitExam(false, isFinish, entity.getMessage());
                            }
                        } else {
                            iExamView.actionSubmitExam(false, isFinish, "数据为空");
                        }

                    }

                    @Override
                    public void onHandleError(int code, String message) {
                        super.onHandleError(code, message);
                        iExamView.actionSubmitExam(false, isFinish, message);
                    }
                });
    }

    /**
     * 提交答案
     *
     * @param bean
     * @param isFinish 提交问卷
     * @param dialog
     */
    public void actionSubmitQuest(AnswerBean bean, final boolean isFinish, Dialog dialog) {
        final ExamContract.IExamView iExamView = getMyView();
        if (iExamView == null)
            return;
        setPd(dialog);
        Map paramsMap = new HashMap();
//        TokenManagerDef.work(() ->
                model.actionSubmitQuest(bean).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new BaseObserverStringNormalHttp(this, pd) {
                    @Override
                    public void onHandleSuccess(String stResult) {
                        BaseEntity entity = JSON.parseObject(stResult, BaseEntity.class);
                        if (entity != null) {
                            if (entity.isOk()) {
                                iExamView.actionSubmitQuest(true, isFinish, "提交成功");
                            } else {
                                iExamView.actionSubmitQuest(false, isFinish, entity.getMessage());
                            }
                        } else {
                            iExamView.actionSubmitQuest(false, isFinish, "数据为空");
                        }
                    }

                    @Override
                    public void onHandleError(int code, String message) {
                        super.onHandleError(code, message);
                        iExamView.actionSubmitQuest(false, isFinish, message);

                    }
                });
    }


    /**
     * 试题结果列表
     */
    public void getExamResultList(String examId, Dialog dialog) {
        final ExamContract.IExamView iExamView = getMyView();
        if (iExamView == null)
            return;
        setPd(dialog);
        TokenManagerDef.work(() -> model.getExamResultList(examId)).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<List<ExamResultItemBean>>(this, pd) {
                    @Override
                    public void onHandleSuccess(List<ExamResultItemBean> bean) {
                        iExamView.setExamResultList(true, bean, "请求成功");
                    }

                    @Override
                    public void onHandleError(int code, String message) {
                        super.onHandleError(code, message);
                        iExamView.setExamResultList(false, null, message);
                    }
                });
    }

    /**
     * 获取考试结果
     */
    public void getExamResultDetails(String examId, Dialog dialog) {
        final ExamContract.IExamView iExamView = getMyView();
        if (iExamView == null)
            return;
        setPd(dialog);
        Map paramsMap = new HashMap();
        TokenManagerDef.work(() -> model.getExamResultDetails(examId)).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<ExamResultDetailsBean>(this, pd) {
                    @Override
                    public void onHandleSuccess(ExamResultDetailsBean bean) {
                        iExamView.setExamResultDetails(true, bean, "请求成功");
                    }

                    @Override
                    public void onHandleError(int code, String message) {
                        super.onHandleError(code, message);
                        iExamView.setExamResultDetails(false, null, message);
                    }
                });
    }

    //获取考试列表
    public void getExamQuestList(final int pageNo, int pageSize, String type, Map paramsMap, Dialog dialog) {
        final ExamContract.IExamView iExamView = getMyView();
        if (iExamView == null)
            return;
        if (paramsMap == null) {
            paramsMap = new HashMap();
        }
        setPd(dialog);
        paramsMap.put("pageNo", pageNo);
        paramsMap.put("pageSize", pageSize);
        if (type.equals("1")) {//在线考试
            paramsMap.put("isPurchase", "0");//是否购买,1已购买
            paramsMap.put("isFree", "1");
        } else {
            paramsMap.put("isPurchase", "1");//是否购买,1已购买
        }
    }


    public void getSignUpList(final int pageNo, int PageSize) {
        final ExamContract.ISignView iView = (ExamContract.ISignView) obtainView();
        if (iView == null)
            return;
        TokenManagerDef.work(() -> model.getSignUpList(pageNo, PageSize)).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<List<SignUpBean>>(this, pd) {
                    @Override
                    public void onHandleSuccess(List<SignUpBean> bean) {
                        iView.setSignUpList(true, pageNo, bean);
                    }

                    @Override
                    public void onHandleError(int code, String message) {
                        super.onHandleError(code, message);
                        iView.setSignUpList(false, pageNo, null);
                    }
                });
    }

    public void getMyExam(final int pageNo, int pageSize, Map map) {
        final ExamContract.IMyExam iView = (ExamContract.IMyExam) obtainView();
        if (iView == null)
            return;
        map.put("pageNo", pageNo);
        map.put("pageSize", pageSize);
        TokenManagerDef.work(() -> model.getMyExam(map)).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<List<ExamBean>>(this, pd) {
                    @Override
                    public void onHandleSuccess(List<ExamBean> bean) {
                        iView.setMyExamList(true, pageNo, bean);
                    }

                    @Override
                    public void onHandleError(int code, String message) {
                        super.onHandleError(code, message);
                        iView.setMyExamList(false, pageNo, null);
                    }
                });
    }

    /*model.getExamList(paramsMap).compose(composeFunction)
            .subscribe(new BaseObserverEntityNormalHttp<List<ExamBean>>(this,pd) {
                @Override
                public void onHandleSuccess(List<ExamBean> beans) {
                    iExamView.setExamList(true,pageNo,beans,"请求成功");
                }

                @Override
                public void onHandleError(int code, String message) {
                    super.onHandleError(code, message);
                    iExamView.setExamList(false,pageNo,null,"请求成功");
                }
            });*/
    private ExamContract.IExamView getMyView() {
        return (ExamContract.IExamView) obtainView();
    }
}
